$(function () {
    // 配置路径
    var pageUrl = config.urlAnaConfig.testPath;
    // 获取Cookie
    function getCookie(name){
        var strcookie = document.cookie;//获取cookie字符串
        var arrcookie = strcookie.split("; ");//分割
    //       console.log(arrcookie);
        //遍历匹配
        for ( var i = 0; i < arrcookie.length; i++) {
            var arr = arrcookie[i].split("=");
                if (arr[0] ==name){
                    return arr[1];
                }
        }
        return "";
    }
    var name = "tokenval";
    // tiken值
    var cookie_value = getCookie(name);
    console.log(cookie_value);
    var name1 = "userName";
    // 用户名值
    var cookie_value1 = getCookie(name1);
    $(".userName").html(cookie_value1);
    $(".nav-usename").html(cookie_value1);

    // 下拉数据
    $.ajax({
        url: pageUrl+'/api/businessinsight/getplatforms',
        type: 'POST',
        dataType: 'json',
        headers: {
            Authorization: 'Bearer '+cookie_value
        },
        success: function (data) {
            var msg = '';
            if (data) {
                if (data.code === 200) {
    //                  店铺名称
                    
                    var details1 = data.data.platforms;
                   
                
                    var details1F = $(".details1").next("ul");
                    var details1G = $(".details2").next("ul");
            
            
                    /*for in 循环*/
                     for(var i in details1){
                        var text="<li>" +details1[i]+"</li>";
                 
                        details1F.append(text);
                        details1G.append(text);
                     };
                }
            }
        },
        error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }

    });
    $('.details1').change(function(){
        $(".details2 option:gt(0)").remove();
        $(".details2").attr("disabled",false);
        var details1D= $(this).val();
        console.log(details1D);
        $.ajax({
            url: pageUrl+'/api/businessinsight/getstorebyplatform',
            type: 'POST',
            dataType: 'json',
            data:{
                platform:details1D
            },
            headers: {
                Authorization: 'Bearer '+cookie_value
            },
            success: function (data) {
                var msg = '';
                if (data) {
                    if (data.code === 200) {
        //                  店铺名称
                        var details2 = data.data.stores;
                        console.log(details2);
                        var details2F = $(".details2");
                        var mydetails2 = details2;
                        /*for in 循环*/
                         for(var i in mydetails2){
                             var text="<option value='" + mydetails2[i] + "'>" + mydetails2[i] + " </option>";
                             details2F.append(text);
                         };

                    }
                }
            },
            error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }

        });
    });
    // 图表
    $.ajax({
        url: pageUrl+'/api/common/getaccesstoken',
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            var msg = '';
            if (data) {
                var accessToken = data.data.accessToken.value;
                //alert(12)
                console.log(accessToken);
                var url1 = "https://app.powerbi.cn/reportEmbed?reportId=ca579f3b-68b8-41e5-840f-6f8ccb9ef097&groupId=13e42e94-3404-46d8-b605-228c35984df3&pageName=ReportSection8b14e250ed979920823c";
                var url2 = "https://app.powerbi.cn/reportEmbed?reportId=ca579f3b-68b8-41e5-840f-6f8ccb9ef097&groupId=13e42e94-3404-46d8-b605-228c35984df3&pageName=ReportSection3e4d2f056c664380097d";
                shineng(accessToken,url1);
                gaikuang(accessToken,url2);
            }
        },
        error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }

    });
    // 确定
    // 图形重置
    var pingjieIndex1 = 0;
    var pingjieIndex2 = 0;
    $(".application").on("click",function () {
        var details1Data = $(".details1").val();
        var detai2s1Data = $(".details2").val();
        
        var startDate = $(".tjTimeS").html();
        var endDate = $(".tjTimeE").html();
        //alert(startDate)
        if (details1Data == 0 || detai2s1Data==0){
            alert("请选择完整选项");
            return false;
        };
        // 拼接图形盒子1
        pingjieIndex1++;
        var tu1Id = "reportContainer";
        tu1Id = tu1Id + pingjieIndex1;
        console.log(tu1Id);
        var arrtx1 = [];
        arrtx1.push("<div class='tu11 no-border'" + "id=" +tu1Id +">");
        arrtx1.push("</div>");
        str1 = arrtx1.join('');
        $(".tu1").append(str1);
        // 删除其他元素
        $('.tu1'+' #'+tu1Id).siblings().remove();

        // 拼接图形盒子2
        pingjieIndex2++;
        var tu2Id = "reportContainer1";
        tu2Id = tu2Id + pingjieIndex2;
        console.log(tu2Id);
        var arrtx2 = [];
        arrtx2.push("<div class='tu22 no-border'" + "id=" +tu2Id +">");
        arrtx2.push("</div>");
        str2 = arrtx2.join('');
        $(".tu2").append(str2);
        // 删除其他元素
        $('.tu2'+' #'+tu2Id).siblings().remove();

        var url1 = "https://app.powerbi.cn/reportEmbed?reportId=ca579f3b-68b8-41e5-840f-6f8ccb9ef097&groupId=13e42e94-3404-46d8-b605-228c35984df3&pageName=ReportSection8b14e250ed979920823c";
        var url2 = "https://app.powerbi.cn/reportEmbed?reportId=ca579f3b-68b8-41e5-840f-6f8ccb9ef097&groupId=13e42e94-3404-46d8-b605-228c35984df3&pageName=ReportSection3e4d2f056c664380097d";
        url1 = url1+"&filter=date_table/data_date ge"+" "+startDate+" " +" and date_table/data_date le"+" "+ endDate  +" and platform_table/code eq"+" '"+details1Data+"' "+"and store_table/code eq "+"'"+detai2s1Data+"'";
        url2 = url2+"&filter=date_table/data_date ge"+" "+startDate+" " +" and date_table/data_date le"+" "+ endDate  +" and platform_table/code eq"+" '"+details1Data+"' "+"and store_table/code eq "+"'"+detai2s1Data+"'";
        $.ajax({
            url: pageUrl+'/api/common/getaccesstoken',
            type: 'POST',
            dataType: 'json',
            success: function (data) {
                var msg = '';
                if (data) {
                   
                    var accessToken = data.data.accessToken.value;
                    shineng(accessToken,url1);
                    gaikuang(accessToken,url2);
                }
            },
            error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }
        });
    });

    function shineng(accessToken,url1) {
        // Read embed application token from Model
        var accessToken = accessToken;
        // Read embed URL from Model
        var embedUrl = url1;
        console.log(embedUrl);
        // Read report Id from Model
        var embedReportId = "ca579f3b-68b8-41e5-840f-6f8ccb9ef097";
        // Get models. models contains enums that can be used.
        var models = window['powerbi-client'].models;
        // Embed configuration used to describe the what and how to embed.
        // This object is used when calling powerbi.embed.
        // This also includes settings and options such as filters.
        // You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
        var config = {
            type: 'report',
            // tokenType: models.TokenType.Embed,
            accessToken: accessToken,
            embedUrl: embedUrl,
            id: embedReportId,
            viewMode: models.ViewMode.View,
            // filters: filter,
            permissions: models.Permissions.All ,
            settings: {
                filterPaneEnabled: false,
                navContentPaneEnabled: false
            }
        };
        // Get a reference to the embedded report HTML element
        var tu22 = $('.tu22')[0];
        if ("" != "") {
            $("#RLS").prop('checked', true);
            $("#RLSdiv").show();
        }
        else
        {
            $("#RLS").prop('checked', false);
            $("#RLSdiv").hide();
        }
        if ("False" == "True") {
            $("#noRLSdiv").hide();
            $("#RLS").removeAttr("disabled");
            $("#RLS").change(function () {
                if ($(this).is(":checked")) {
                    $("#RLSdiv").show(300);
                } else {
                    $("#RLSdiv").hide(200);
                }
            });
        }
        else
        {
            $("#noRLSdiv").show();
        }
        var report = powerbi.embed(tu22, config);
    };
    function gaikuang(accessToken,url2) {
        // Read embed application token from Model
        var accessToken = accessToken;
        // Read embed URL from Model
        var embedUrl = url2;
        console.log(embedUrl);
        // Read report Id from Model
        var embedReportId = "ca579f3b-68b8-41e5-840f-6f8ccb9ef097";
        // Get models. models contains enums that can be used.
        var models = window['powerbi-client'].models;
        // Embed configuration used to describe the what and how to embed.
        // This object is used when calling powerbi.embed.
        // This also includes settings and options such as filters.
        // You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
        var config = {
            type: 'report',
            // tokenType: models.TokenType.Embed,
            accessToken: accessToken,
            embedUrl: embedUrl,
            id: embedReportId,
            viewMode: models.ViewMode.View,
            // filters: filter,
            permissions: models.Permissions.All ,
            settings: {
                filterPaneEnabled: false,
                navContentPaneEnabled: false
            }
        };
        // Get a reference to the embedded report HTML element
        var tu11 = $('.tu11')[0];
        if ("" != "") {
            $("#RLS").prop('checked', true);
            $("#RLSdiv").show();
        }
        else
        {
            $("#RLS").prop('checked', false);
            $("#RLSdiv").hide();
        }
        if ("False" == "True") {
            $("#noRLSdiv").hide();
            $("#RLS").removeAttr("disabled");
            $("#RLS").change(function () {
                if ($(this).is(":checked")) {
                    $("#RLSdiv").show(300);
                } else {
                    $("#RLSdiv").hide(200);
                }
            });
        }
        else
        {
            $("#noRLSdiv").show();
        }
        var report = powerbi.embed(tu11, config);
    };

      
});